package com.artgogo.artgogoerp.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ArtgogoUtil {
    // 常值参数设定
    public static final int TEST_URL = 0;
    public static final int LOGIN_URL = 1;
    public static final int MOVE_URL = 2;
    public static final int RECORD_URL = 3;
    
    // Only display the first 500 characters of the retrieved
    // web page content.
    private static final int len = 500;
    
    private static final String testUrl = "http://www.artgogo.com/cms_artgogo/storehouse/android_test.jsp";
    private static final String loginUrl = "http://www.artgogo.com/cms_artgogo/storehouse/android_login.jsp";
    private static final String moveUrl = "http://www.artgogo.com/cms_artgogo/storehouse/android_move.jsp";
    private static final String recordUrl = "http://www.artgogo.com/cms_artgogo/storehouse/android_test.jsp";
    
    public static JSONObject getConnection(int urlCode, JSONObject data) throws IOException {
        InputStream is;
        String contentAsString;
        HttpURLConnection conn = null;
        JSONObject responseData = null;
        
        String urlString = getUrl(urlCode);
        if(urlString != null){
            try {
                
                URL url = new URL(urlString);
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 );// milliseconds 
                conn.setConnectTimeout(15000 );// milliseconds 
                conn.setDoOutput(true);
                conn.setChunkedStreamingMode(0);
    
                // Starts the query
                conn.connect();
                PrintWriter out = new PrintWriter(conn.getOutputStream());
                out.print(JSON2Request.parseJSON(data));
                out.flush();
                out.close();
                
                int responseCode = conn.getResponseCode();
    
                is = conn.getInputStream();
                // Convert the InputStream into a string
                contentAsString = readIt(is, len);
                // Makes sure that the InputStream is closed after the app is
                // finished using it.
                is.close();
                responseData = JSON.parseObject(contentAsString);
                responseData.put("responseCode", responseCode);
            } finally {
                conn.disconnect();
            }
        }
        return responseData;
    }
    
    // Reads an InputStream and converts it to a String.
    public static String readIt(InputStream stream, int len) throws IOException {
        Reader reader;
        reader = new InputStreamReader(stream, "UTF-8");
        char[] buffer = new char[len];
        int size = reader.read(buffer);
        return new String(buffer,0,size);
    }
    
    private static String getUrl(int urlCode){
        switch(urlCode){
        case TEST_URL:
            return testUrl;
        case LOGIN_URL:
            return loginUrl;
        case MOVE_URL:
            return moveUrl;
        case RECORD_URL:
            return recordUrl;
        default:
            return null;
        }
    }
    
    public static String getResponseStatus(int status){
        if(status == HttpURLConnection.HTTP_OK)return "OK";
        else return "UNKNOWN";
    }
    
    private static String formatJSON(String JSONStr){
        System.out.println(JSONStr.length());
        String formatResult = null;
        Pattern JSONPattern = Pattern.compile("\\S+");
        Matcher mc = JSONPattern.matcher(JSONStr);
        
        if(mc.find()){
            formatResult = mc.group();
            System.out.println(formatResult);
        }
        return formatResult;
    }
}
